package cn.it.ygh.controller;


import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.it.ygh.pojo.House;
import cn.it.ygh.pojo.Order;
import cn.it.ygh.pojo.User;
import cn.it.ygh.service.UserService;
import cn.it.ygh.utils.Md5Util;


@Controller(value="UserController")
public class UserController {
	

	@Resource
	public UserService userservice;
	
	@RequestMapping(value = "/userLogin.do",method=RequestMethod.POST)
	public ModelAndView login(User u,HttpSession session) throws NoSuchAlgorithmException, UnsupportedEncodingException{
		
		ModelAndView mav = new ModelAndView();
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time=format.format(date);
		session.setAttribute("time", time);
		String userAccount = u.getUserAccount();  //获取用户登录界面的用户名
		String pwd = u.getUserPwd();        //获取用户登录界面输入的密码
		String md5Pwd = userservice.getPwdByUserAccount(userAccount);
		String md5Pwd1 = Md5Util.getMD5Pwd(pwd);
		u.setUserPwd(md5Pwd);
		if(md5Pwd1.equals(md5Pwd)){
			User user = userservice.login(u);
			if(user!=null)
			{
				mav.setViewName("../users/jsp/index");
				session.setAttribute("u", user);
				return mav;
			}
			else
			{
				mav.setViewName("../users/jsp/UserLogin");
				return mav;
			}
		}
		else{
			mav.setViewName("../users/jsp/UserLogin");
			return mav;
		}
	}
	
	
	@RequestMapping(value = "/userRegister.do",method=RequestMethod.POST)
	public ModelAndView register(User u){
		
		ModelAndView mav = new ModelAndView();
		String  pwd = Md5Util.getMD5Pwd(u.getUserPwd());
		u.setUserPwd(pwd);
		userservice.register(u);
		u.setUserPwd(pwd);
		mav.setViewName("../users/jsp/UserLogin");
		return mav;
	}
	
	
	@RequestMapping(value = "/userModify.do",method=RequestMethod.POST)
	public ModelAndView modify(User user,@RequestParam("userPwd1") String userPwd1,@RequestParam("userPwd") String userPwd, @RequestParam("userPwd3") String userPwd3,HttpSession session){
		
		ModelAndView mav = new ModelAndView();
		User u = (User) session.getAttribute("u");
		String md5Pwd1 = Md5Util.getMD5Pwd(userPwd1);     //将输入的原密码转化成对应的md5密码
		System.out.println(md5Pwd1);
		String userAccount = user.getUserAccount();       //获取输入的用户名
		String md5pwd = userservice.getPwdByUserAccount(userAccount);    //根据用户名获取数据库中的MD5密码
		System.out.println(md5pwd);
		String userPwd4= u.getUserPwd();
		System.out.println(userPwd4);       //获取数据库中的MD5密码
		if(userPwd4.equals(md5Pwd1))
		{
			if(userPwd.equals(userPwd3))
			{
				String md5Pwd2 = Md5Util.getMD5Pwd(userPwd);
				user.setUserPwd(md5Pwd2);
				userservice.modify(user);
				user.setUserName(u.getUserName());
				user.setUserIdentity(u.getUserIdentity());
				user.setUserTime(u.getUserTime());
				session.setAttribute("u", user);
				mav.setViewName("../users/jsp/MyInfo");
			}
			else{
				mav.setViewName("../users/jsp/UserModify");
			}
		}
		else{
			mav.setViewName("../users/jsp/UserModify");
		}
		return mav;	
	}
	
	@RequestMapping(value = "/getRecordData.do",method = RequestMethod.GET)
	@ResponseBody
	public Map<Object,Object> getRecordData(@RequestParam(value = "pageStart") int pageStart,@RequestParam(value="pageSize") int pageSize,HttpSession session){
		
		User u = (User) session.getAttribute("u");
		String userAccount = u.getUserAccount();
		System.out.println(userAccount);
		List<House> recordList = userservice.findAllRecords((pageStart-1)*pageSize,pageSize,userAccount);
		session.setAttribute("record", recordList);
		int total = userservice.findRecordsTotal(userAccount);
		Map<Object,Object> info = new HashMap<Object,Object>();
		info.put("data",recordList);
		info.put("total",total);
		return info;
	}
	
	@RequestMapping(value="/getSingleRecord.do")
	public void getRecordsById(@RequestParam(value="houseId") int houseId, HttpSession session){
		House record = userservice.getRecordById(houseId);
		session.setAttribute("singleRecord", record);

	}
	
	@RequestMapping(value = "/getOrdersData.do",method = RequestMethod.GET)
	@ResponseBody
	public Map<Object,Object> getOrderData(@RequestParam(value = "pageStart") int pageStart,@RequestParam(value="pageSize") int pageSize){
		
		List<Order> orderList = userservice.findAllOrders((pageStart-1)*pageSize,pageSize);
		int total = userservice.findOrdersTotal();
		Map<Object,Object> info = new HashMap<Object,Object>();
		info.put("data",orderList);
		info.put("total",total);
		return info;
	}
	
	@RequestMapping(value = "/recordModify.do",method=RequestMethod.POST)
	public ModelAndView recordModify(House house){
		
		ModelAndView mav = new ModelAndView();
		userservice.recordModify(house);
		mav.setViewName("../users/jsp/MyJsp");
		return mav;	
	}
	
	@RequestMapping("/deleteRecord.do")
	public ModelAndView deleteUser(@RequestParam(value = "houseId") Integer houseId){
		
		ModelAndView mav = new ModelAndView();
		userservice.delete(houseId);
		mav.setViewName("../users/jsp/Myjsp");
		return mav;
	}
	
	@RequestMapping(value="/ModifyUserImg.do",method=RequestMethod.POST)
	public ModelAndView updateModifyImg(User user,HttpSession session) {
		
		User u = (User) session.getAttribute("u");
		ModelAndView modelAndView = new ModelAndView();
		u.setUserImg(user.getUserImg());
		session.setAttribute("u", u);
		userservice.userImg(u);
		modelAndView.setViewName("../users/jsp/MyJsp");
		return modelAndView;		
		}
	
}
